<?php
class ProductService extends My_Service
{   
	public function buildQueryData($orderBy = "", $orderDirection="",
	$key = "", $field = "", $params = null)
	{
		if(empty($orderBy))
		{
			$orderBy = "ordering";
		}
		return parent::buildQueryData($orderBy,$orderDirection,$key,$field,$params);
	}
	
    public function check()
    {
		//validate Product Name
		$titleVald = new Zend_Validate_StringLength(3, 255);
		if(!$titleVald->isValid($_POST['productName']))
		{
			$this->_errorList["productName"] = My_Message::get("S002|productName|10|255");
			return false;
		}
		
		//validate Summary
		$summaryVald = new Zend_Validate_StringLength(5);
		if(!$summaryVald->isValid($_POST['summary']))
		{
			$this->_errorList["summary"] = My_Message::get("S003|Summary|5");
			return false;
		}
		
		//validate category
		if(empty($_POST['categoryId']))
		{
			$this->_errorList["categoryId"] = My_Message::get("S001|Category");
		}
		
		//validate description
		$bodyVald = new Zend_Validate_StringLength(10);
		if(!$bodyVald->isValid($_POST['description']))
		{
			$this->_errorList["description"] = My_Message::get("S003|Description|10");
		}
		
		//validate image
		if(!empty($_FILES['imageURL']['name']))
		{
			$adapter = new Zend_File_Transfer_Adapter_Http();

			$adapter->addValidator('IsImage', false);
			if(!$adapter->isValid())
			{
				$this->_errorList["imageURL"] = My_Message::get("S004");
				return false;
			}

			$uploadDir = PRODUCT_IMG_UPLOAD_PATH;
			if(!is_readable($uploadDir))
			{
				$this->_errorList["imageURL"] = My_Message::get("S005|Image");
				return false;
			}
			$rand = date("Ymdhis").rand()."_";
			$fileName = $rand.$_FILES ['imageURL']['name'];
			$res = move_uploaded_file($_FILES ['imageURL']['tmp_name'],$uploadDir."/".$fileName);
			if (! $res) {
				$this->_errorList["imageURL"] = My_Message::get("S005|Image");
				return false;
			}
			$_POST['imageURL']['name'] = $fileName;
		}
		
		if(empty($this->_errorList) == false)
		{
			return false;
		}
		return true;
    }
    
	public function bind()
	{
		$product = new ProductDTO( );
		if(!empty($_POST['productId']))//case edit
		{
			$product = $this->load($_POST['productId']);
		}
		
		$product->productName = $_POST['productName'];
		$product->summary = $_POST['summary'];
		$product->description = $_POST['description'];
		$product->status = $_POST['status'];
		$product->categoryId = $_POST['categoryId'];
		
		if(empty($_POST['productId']))//case insert
		{
			$product->creationTime = date ( "Y-m-d h:i:s" );
			$product->createdBy = 2;
			$product->ordering = 1;
		}
		
		if (! empty ( $_POST ['imageURL']['name'] )) 
		{
			if(!empty($_POST['productId']))//case edit, remove old images
			{
				$uploadDir = PRODUCT_IMG_UPLOAD_PATH;
				$imgFile = $uploadDir.$product->imageURL;
				$thumbFile = $uploadDir.$product->thumbnailURL;
				@unlink($imgFile);
				@unlink($thumbFile);	
			}
			
			$product->imageURL = basename ( $_POST ['imageURL'] ['name'] );
			$uploadDir = PRODUCT_IMG_UPLOAD_PATH;
			$product->thumbnailURL = My_Utility::createThumb ( $uploadDir . $product->imageURL, 99, 99 );
			
		}
		
		return $product;
	}
    
    public function store()
    {
    	//check input data
    	if(!$this->check())
    	{
			return false;
    	}
    	
    	//bind data
    	$product = $this->bind();
    	
    	//save data
    	$this->save($product);
    	return true;
    }
    
	public function remove($productId)
	{
		$product = $this->load($productId);
		$uploadDir = PRODUCT_IMG_UPLOAD_PATH;
		$imgFile = $uploadDir.$product->imageURL;
		$thumbFile = $uploadDir.$product->thumbnailURL;
		@unlink($imgFile);
		@unlink($thumbFile);
		parent::remove($productId);
	}
	
	public function loadCategoryOptions()
	{
		$categoryDAO = new ProductCategoryDAO();
		
		$categoryIdList = $categoryDAO->loadCol('categoryId');
		$categoryNameList = $categoryDAO->loadCol('categoryName');
		
		array_unshift($categoryIdList,0);
		array_unshift($categoryNameList,"- Select Category -");
		
		return array_combine($categoryIdList,$categoryNameList);
	}
	
	public function getRandomProducts($num)
	{
		return $this->getDAO()->getRandomProducts($num);
	}
    
	public function getPublish($catId)
	{
		return $this->getDAO()->getPublish($catId);
	}
	
	public function getTopProducts()
	{
		return $this->getDAO()->getTopProducts();
	}
	
	public function getProductsWithType($type)
	{
		$langCode = My_Translate::getLanguageCode();
		if($langCode == "vn")
		{
			if($type == "phugia")
	    	{
	    		$catId = 4;
	    	}
	    	if($type == "thietbi")
	    	{
	    		$catId = 9;
	    	}
	    	if($type == "tieubieu")
	    	{
	    		$catId = 10;
	    	}
			if($type == "giacam")
	    	{
	    		$catId = 1;
	    	}
	    	if($type == "giasuc")
	    	{
	    		$catId = 2;
	    	}
	    	if($type == "vatnuoikhac")
	    	{
	    		$catId = 3;
	    	}
		}
		else
		{
			if($type == "phugia")
	    	{
	    		$catId = 8;
	    	}
	    	if($type == "thietbi")
	    	{
	    		$catId = 11;
	    	}
	    	if($type == "tieubieu")
	    	{
	    		$catId = 12;
	    	}
			if($type == "giacam")
	    	{
	    		$catId = 6;
	    	}
	    	if($type == "giasuc")
	    	{
	    		$catId = 5;
	    	}
	    	if($type == "vatnuoikhac")
	    	{
	    		$catId = 7;
	    	}
		}
		
		return $this->getDAO()->getPublish($catId);
	}
	
	public function getOtherProducts($product_id)
	{
		$product = $this->load($product_id);
		return $this->getPublish($product->categoryId);
	}
}
?>